package view.waiters;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import com.eltima.components.ui.DatePicker;

import entiey.Waiter;
import service.WaiterService;
import service.impl.WaiterServiceImpl;
import util.CastUtil;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.Date;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;
import java.awt.Color;

public class waiter_add_update extends JFrame {

	private JPanel contentPane;
	private JTextField text_waiterNo;
	private JTextField text_name;
	private JTextField text_gender;
	private JTextField text_phone;
//	private JTextField textField_5;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					waiter_add_update frame = new waiter_add_update();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public waiter_add_update() {
		setTitle("服务员信息");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 417, 436);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u4FE1\u606F\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("工 号：");
		lblNewLabel.setBounds(57, 46, 54, 15);
		panel.add(lblNewLabel);
		
		text_waiterNo = new JTextField();
		text_waiterNo.setBounds(121, 43, 145, 21);
		panel.add(text_waiterNo);
		text_waiterNo.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("姓 名：");
		lblNewLabel_1.setBounds(57, 94, 54, 15);
		panel.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("性 别：");
		lblNewLabel_2.setBounds(57, 137, 54, 15);
		panel.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("电 话：");
		lblNewLabel_3.setBounds(57, 178, 54, 15);
		panel.add(lblNewLabel_3);
		
		JLabel lblNewLabel_4 = new JLabel("入职时间：");
		lblNewLabel_4.setBounds(57, 222, 65, 15);
		panel.add(lblNewLabel_4);
		
		JLabel lblNewLabel_5 = new JLabel("备 注：");
		lblNewLabel_5.setBounds(57, 270, 54, 15);
		panel.add(lblNewLabel_5);
		
		text_name = new JTextField();
		text_name.setBounds(121, 91, 145, 21);
		panel.add(text_name);
		text_name.setColumns(10);
		
		text_gender = new JTextField();
		text_gender.setBounds(121, 134, 145, 21);
		panel.add(text_gender);
		text_gender.setColumns(10);
		
		text_phone = new JTextField();
		text_phone.setBounds(121, 178, 145, 21);
		panel.add(text_phone);
		text_phone.setColumns(10);
		
		
		dp =new DatePicker();
		
//		textField_5 = new JTextField();
		dp.setBounds(121, 219, 145, 21);
		
		dp.getInnerTextField().setText(CastUtil.format(new Date()));
		panel.add(dp);
		
		text_moreInfo = new JTextPane();
		text_moreInfo.setBounds(121, 270, 167, 63);
		panel.add(text_moreInfo);
//		textField_5.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);
		
		JButton btnNewButton = new JButton("确定");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
				WaiterService waService = new WaiterServiceImpl();
				
				if(up_waiter!=null) {//不等于空说明对象有值 ，走修改
					
					up_waiter.setName(text_name.getText());
					up_waiter.setGender(text_gender.getText());
					up_waiter.setPhone(text_phone.getText());
					String InTime = dp.getInnerTextField().getText();//入职时间
					try {
						up_waiter.setTime(CastUtil.parse(InTime));
					} catch (ParseException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					up_waiter.setMoreInfo(text_moreInfo.getText());//备注
					
					
					if(waService.updateWaiter(up_waiter)) {
						JOptionPane.showMessageDialog(waiter_add_update.this, "保存成功");
						dispose();//关闭当前窗体
					}else {
						JOptionPane.showMessageDialog(waiter_add_update.this, "保存失败");
					}
					
				}else {//为空，则添加服务员
					
					Waiter w = new Waiter();
					w.setWaiterNo(Integer.parseInt(text_waiterNo.getText()));
					w.setName(text_name.getText());
					w.setGender(text_gender.getText());
					w.setPhone(text_phone.getText());
					String InTime = dp.getInnerTextField().getText();//入职时间
					try {
						w.setTime(CastUtil.parse(InTime));
					} catch (ParseException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					w.setMoreInfo(text_moreInfo.getText());//备注
					
					if(waService.saveWaiter(w)) {
						JOptionPane.showMessageDialog(waiter_add_update.this, "保存成功");
						dispose();//关闭当前窗体
					}else {
						JOptionPane.showMessageDialog(waiter_add_update.this, "保存失败");
					}
				}
				
				
				
				
				
			}
		});
		panel_1.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("取消");
		panel_1.add(btnNewButton_1);
	}
	private Waiter up_waiter = null;//要修改的服务员对象
	private JTextPane text_moreInfo;
	private DatePicker dp;
	
	//代参构造，id服务员编号
	public waiter_add_update(int id) {
		this();//先执行无参构造
		up_waiter= new WaiterServiceImpl().findById(id);//获取要修改的服务员对象信息
		text_waiterNo.setText(CastUtil.castString(up_waiter.getWaiterNo()));
		text_name.setText(up_waiter.getName());
		text_gender.setText(up_waiter.getGender());
		text_phone.setText(up_waiter.getPhone());
		dp.getInnerTextField().setText(CastUtil.format(up_waiter.getTime()));
		text_moreInfo.setText(up_waiter.getMoreInfo());
	}
	
	
	
	
}
